//
//  MineAddressRootViewController.m
//  shopping
//
//  Created by 马田成 on 2017/9/1.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "MineAddressRootViewController.h"

#import "MineAddressNewViewController.h"
#import "MineAddressRootTableViewCell.h"
@interface MineAddressRootViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;


@end

@implementation MineAddressRootViewController

- (void)viewWillAppear:(BOOL)animated{
    
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
    [self creationDate];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationView];
    [self regeditCell];
    
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    [self setTitle:@"管理收货地址"];
    
    UIView *viewOf = [[UIView alloc] init];
    viewOf.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1);
    [self.view addSubview:viewOf];
    viewOf.backgroundColor = [UIColor colorWithRed:204 / 255.0f green:204 / 255.0f  blue:204 / 255.0f  alpha:1];
    
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 1, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64);

    self.tableView.backgroundColor = getColor(@"eeeeee");
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
   // self.tableView.estimatedRowHeight = 150;
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    
    
    [self.view addSubview:self.tableView];

//    UIButton *buttonAdd = [[UIButton alloc] init];
//    
//    buttonAdd.frame = CGRectMake(DEF_SCREEN_WIDTH - 50, 0, 40, 40);
//    
//    
//    [buttonAdd setTitle:@"保存" forState:UIControlStateNormal];
//    [buttonAdd setTitleColor:getColor(@"222222") forState:UIControlStateNormal];
//    [buttonAdd addTarget:self action:@selector(messageClick) forControlEvents:UIControlEventTouchUpInside];
//    
//    UIBarButtonItem *rightItme = [[UIBarButtonItem alloc]initWithCustomView:buttonAdd];
//    self.navigationItem.rightBarButtonItem = rightItme;
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 300)];
    
    self.tableView.tableFooterView.userInteractionEnabled = YES;
    UIButton *buttonAddress = [[UIButton alloc] init];
    
    [self.tableView.tableFooterView addSubview:buttonAddress];
    buttonAddress.backgroundColor = getColor(@"FFDA44");
    buttonAddress.layer.cornerRadius = 4.0f;
    buttonAddress.layer.masksToBounds = YES;
    buttonAddress.titleLabel.font = [UIFont systemFontOfSize:14];
    [buttonAddress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView.tableFooterView).with.offset(35);
        make.left.equalTo(self.view).with.offset(25);
        make.right.equalTo(self.view).with.offset(-25);
        make.height.offset(40);
        
    }];
    [buttonAddress setTitle:@"添加新地址" forState:UIControlStateNormal];
    [buttonAddress setTitleColor:getColor(@"222222") forState:UIControlStateNormal];
    
    [buttonAddress addTarget:self action:@selector(addressClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}
- (void)creationDate{
    
    _dataArr = [[NSMutableArray alloc] init];
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
   // NSString *token = @"32728a411b83e29b9836262a72477e3e0dac";
    
    [productParams setValue:token forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:adminusergetUserAddress networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        
        if ([[returnData objectForKey:@"status"]intValue]==1) {
            
            _dataArr = [returnData objectForKey:@"data"];
            
            
            
            NSLog(@"!!!!!!!!!!!!!!!%@", _dataArr);
            
            [self.tableView reloadData];
            
            
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
//        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
    
}


- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MineAddressRootTableViewCell" bundle:nil] forCellReuseIdentifier:@"MineAddressRootTableViewCell"];
    
    
    
    
}
#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 130;
    
}

#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
        
    
    MineAddressRootTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MineAddressRootTableViewCell" forIndexPath:indexPath];
        
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    NSMutableDictionary *dic = self.dataArr[indexPath.row];
    
    cell.userName.text = [dic objectForKey:@"userName"];
    cell.userPhone.text = [dic objectForKey:@"userPhone"];
    cell.address.text = [dic objectForKey:@"address"];

    NSInteger diZID = [[dic objectForKey:@"addressId"] integerValue];
    cell.bianJiBtn.tag = diZID;
    cell.shanChuBtn.tag = diZID;
    
    [cell.shanChuBtn addTarget:self action:@selector(shanChuClick:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.addButtonaaaa.tag = diZID;
    
    [cell.addButtonaaaa addTarget:self action:@selector(MoRenClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    if ([[dic objectForKey:@"isDefault"] isEqualToString:@"0"]) {
        
        [cell.isDefaultBtn setBackgroundImage:[UIImage imageNamed:@"shouhuo_meiyou"] forState:UIControlStateNormal];
    }else {
    
        [cell.isDefaultBtn setBackgroundImage:[UIImage imageNamed:@"shouhuo_xuanzhong"] forState:UIControlStateNormal];
    }
    
    [cell callBackOfPicBlock:^{
        
        [self withStr:[dic objectForKey:@"userName"] phone:[dic objectForKey:@"userPhone"] sheng:[dic objectForKey:@"areaName1"] shi:[dic objectForKey:@"areaName2"] qu:[dic objectForKey:@"areaName3"] dizhi:[dic objectForKey:@"address"] dizhiID:[dic objectForKey:@"addressId"]];
        
        
    }];
    
    
    
    return cell;
    
 
}
#pragma mark - ---------- 每个Cell的点击事件 ----------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([self.style isEqualToString:@"1"]) {
        
    }else {
    
        NSString *addressID = [self.dataArr[indexPath.row] objectForKey:@"addressId"];
        
        [self.delegate withString:addressID];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }


}



#pragma mark-------------------------保存--------
- (void)messageClick{

    
}
#pragma  mark -----------添加收货地址------
- (void)addressClick{

    MineAddressNewViewController *vc = [[MineAddressNewViewController alloc] init];
    vc.type = @"1";
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
//删除
- (void)shanChuClick:(UIButton *)sender{

    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    // NSString *token = @"32728a411b83e29b9836262a72477e3e0dac";
    
    [productParams setValue:token forKey:@"token"];
    
    NSString *addID = [NSString stringWithFormat:@"%ld", (long)sender.tag];
    
    [productParams setValue:addID forKey:@"addressId"];
    [[MNetworkHandler sharedInstance]conURL:adminOrderDelAddress networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        
        if ([[returnData objectForKey:@"status"]intValue]==1) {
            
            [self creationDate];
            
            
            
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
//        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];

    
}

//默认收货地址
- (void)MoRenClick:(UIButton *)sender{

    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    // NSString *token = @"32728a411b83e29b9836262a72477e3e0dac";
    
    [productParams setValue:token forKey:@"token"];
    
    NSString *addID = [NSString stringWithFormat:@"%ld", (long)sender.tag];
    
    [productParams setValue:addID forKey:@"addressId"];
    [[MNetworkHandler sharedInstance]conURL:adminOrderDefAddress networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        
        if ([[returnData objectForKey:@"status"]intValue]==1) {
            
            [self creationDate];
            
            
            
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
//        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];

    
    
}

#pragma mark --------------编辑地址----------
- (void)withStr:(NSString *)name phone:(NSString *)phone sheng:(NSString *)sheng shi:(NSString *)shi qu:(NSString *)qu dizhi:(NSString *)dizhi dizhiID:(NSString *)dizhiID{

    
    MineAddressNewViewController *vc = [[MineAddressNewViewController alloc] init];
    vc.name = name;
    vc.phone = name;
    vc.sheng = sheng;
    vc.shi = shi;
    vc.qu = qu;
    vc.dizhi = dizhi;
    vc.type = @"2";
    vc.dizhiID = dizhiID;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
